package main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.File;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import deduplication.carnetContacts;

public class Accueil extends JFrame {
    private File file = null;
    private carnetContacts repertoire;
    
    private BorderLayout layoutMain = new BorderLayout();
    private JPanel panelCenter = new JPanel();
    private JMenuBar menuBar = new JMenuBar();
    private JMenu menuFile = new JMenu();
    private JMenuItem menuFileOpen = new JMenuItem();
    private JMenuItem menuFileDedup = new JMenuItem();
    private JMenuItem menuFileClose = new JMenuItem();
    private JMenuItem menuFileExit = new JMenuItem();
    private JMenu menuHelp = new JMenu();
    private JMenuItem menuHelpAbout = new JMenuItem();
    private JLabel statusBar = new JLabel();

    public Accueil() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setJMenuBar( menuBar );
        this.getContentPane().setLayout( layoutMain );
        panelCenter.setLayout( null );
        this.setSize( new Dimension(400, 300) );
        this.setTitle( "Accueil" );
        menuFile.setText( "File" );
        menuFileOpen.setText( "Load..." );
        menuFileOpen.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { fileOpen_ActionPerformed( ae ); } } );
        menuFileDedup.setText( "Dedup" );
        menuFileDedup.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { fileDedup_ActionPerformed( ae ); } } );
        menuFileClose.setText( "Save..." );
        menuFileClose.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { fileClose_ActionPerformed( ae ); } } );
        menuFileExit.setText( "Exit" );
        menuFileExit.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { fileExit_ActionPerformed( ae ); } } );
        menuHelp.setText( "Help" );
        menuHelpAbout.setText( "About" );
        menuHelpAbout.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { helpAbout_ActionPerformed( ae ); } } );
        statusBar.setText( "" );
        menuFile.add( menuFileOpen );
        menuFile.add( menuFileDedup );
        menuFile.add( menuFileClose );
        menuFile.add( menuFileExit );
        menuBar.add( menuFile );
        menuHelp.add( menuHelpAbout );
        menuBar.add( menuHelp );
        this.getContentPane().add( statusBar, BorderLayout.SOUTH );
        this.getContentPane().add( panelCenter, BorderLayout.CENTER );
    }

    void fileExit_ActionPerformed(ActionEvent e) {
        System.exit(0);
    }

    void fileOpen_ActionPerformed(ActionEvent e) {
        fileOpen of = new fileOpen();
        file = of.askFile(this, "Open",new File("E:\\Documents Guillaume\\Outlook"));
        if (file != null) {
            statusBar.setText( "File " + file.getName()  + " selected." );
            if (repertoire==null) {
                repertoire = new carnetContacts();
            }
            if (!repertoire.load(file)) {
                statusBar.setText("File " + file.getName()  + " loaded ("+ repertoire.size() +" contacts).");
            } else {
                statusBar.setText("Error loading file " + file.getName()  + ".");
            }
        }
        file = null;
    }
    void fileDedup_ActionPerformed(ActionEvent e) {
        statusBar.setText( "Start of deduplication (" + repertoire.sizeTelephones() +" telephone # and "+ repertoire.sizeEmails() +" emails).");        
        repertoire.deduplicate();
        statusBar.setText( "End of deduplication (" + repertoire.sizeTelephones() +" telephone # and "+ repertoire.sizeEmails() +" emails).");        
    }
    
    void fileClose_ActionPerformed(ActionEvent e) {
        fileOpen of = new fileOpen();
        file = of.askFile(this,"Save",new File("E:\\Documents Guillaume\\Outlook"));
        statusBar.setText( "File " + file.getName()  + " selected for saving." );
        repertoire.setSeparator(",");
        repertoire.setStringEncloser("\"");
        repertoire.save(file);
        statusBar.setText(repertoire.size() + " contacts saved in " + file.getName()  + "." );
    }
    
    void helpAbout_ActionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(this, new Accueil_AboutBoxPanel1(), "About", JOptionPane.PLAIN_MESSAGE);
    }
}
